import {createRouter, createWebHashHistory} from 'vue-router'
import routeMixin from "../js/routeMixin"

export default class router {
    router = null
    routes = [
        {
            path: '/',
            name: "body",
            component: () => import('/src/views/Temp.vue'),
            meta: {
                requireAuth: true
            },
            children: [
                {
                    name: "修改密码",
                    path: '/updatePassword',
                    component: () => import('../view/system/updatePass.vue'),
                    meta: {
                        newWindow: true
                    }
                },
                {
                    name: "个人信息",
                    path: '/selfInfo',
                    component: () => import('../view/system/selfInfo.vue'),
                    meta: {
                        newWindow: true
                    }
                },
                {
                    name: "404",
                    path: '/:pathMatch(.*)*',
                    component: ()=> import("../view/system/404.vue")
                }
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('../view/Login.vue'),
            meta: {
                type: "login",
                tourist: true
            }
        },
        {
            path: '/pickExport',
            name: 'pickExport',
            component: () => import('../../src/views/produce/materialBomPickExport.vue'),
            meta: {
                type: "pickExport",
                tourist: true
            }
        },
        {
            path: '/taskExport',
            name: 'taskExport',
            component: () => import('../../src/views/produce/prdTaskExport.vue'),
            meta: {
                type: "taskExport",
                tourist: true
            }
        },
        {
            path: '/supplementExport',
            name: 'supplementExport',
            component: () => import('../../src/views/produce/supplementListExport.vue'),
            meta: {
                type: "supplementListExport",
                tourist: true
            }
        },
        {
            path: '/materialBomInExport',
            name: 'materialBomInExport',
            component: () => import('../../src/views/produce/materialBomInExport.vue'),
            meta: {
                type: "materialBomInExport",
                tourist: true
            }
        },
        {
            path: '/returnInStockExport',
            name: 'returnInStockExport',
            component: () => import('../../src/views/task/returnInStockExport.vue'),
            meta: {
                type: "returnInStockExport",
                tourist: true
            }
        },
        {
            path: '/desaltingInStockExport',
            name: 'desaltingInStockExport',
            component: () => import('../../src/views/task/desaltingInStockExport.vue'),
            meta: {
                type: "desaltingInStockExport",
                tourist: true
            }
        },
        {
            path: '/purInExport',
            name: 'purInExport',
            component: () => import('../../src/views/purchase/purInExport.vue'),
            meta: {
                type: "purInExport",
                tourist: true
            }
        },
        {
            path: '/saleOutExport',
            name: 'saleOutExport',
            component: () => import('../../src/views/sale/saleOutExport.vue'),
            meta: {
                type: "saleOutExport",
                tourist: true
            }
        },
        {
            path: '/otherExport',
            name: 'otherExport',
            component: () => import('../../src/views/other/otherExport.vue'),
            meta: {
                type: "otherExport",
                tourist: true
            }
        },
        {
            path: '/otherOutExport',
            name: 'otherOutExport',
            component: () => import('../../src/views/other/otherOutExport.vue'),
            meta: {
                type: "otherOutExport",
                tourist: true
            }
        },
        {
            path: '/prdColExport',
            name: 'prdColExport',
            component: () => import('../../src/views/produce/prdColExport.vue'),
            meta: {
                type: "prdColExport",
                tourist: true
            }
        },
        {
            path: '/prdColExportProduct',
            name: 'prdColExportProduct',
            component: () => import('../../src/views/produce/prdColExportProduct.vue'),
            meta: {
                type: "prdColExportProduct",
                tourist: true
            }
        },
        {
            path: '/purOrderExport',
            name: 'purOrderExport',
            component: () => import('../../src/views/purchase/purOrderExport.vue'),
            meta: {
                type: "purOrderExport",
                tourist: true
            }
        },
        {
            path: '/baiduMapTest',
            name: 'baiduMapTest',
            component: () => import('../../src/views/basic/baiduMapTest.vue'),
            meta: {
                type: "baiduMapTest",
                tourist: true
            }
        },
        {
            path: '/otherOutApplyExport',
            name: 'otherOutApplyExport',
            component: () => import('../../src/views/other/otherOutApplyExport.vue'),
            meta: {
                type: "otherOutApplyExport",
                tourist: true
            }
        },
        {
            path: '/syncKingdeeTimeDialog',
            name: 'syncKingdeeTimeDialog',
            component: () => import('../../src/views/components/syncKingdeeTimeDialog.vue'),
            meta: {
                type: "syncKingdeeTimeDialog",
                tourist: true
            }
        },
        {
            path: '/syncKingdeeTimeDialogBn',
            name: 'syncKingdeeTimeDialogBn',
            component: () => import('../../src/views/components/syncKingdeeTimeDialogBn.vue'),
            meta: {
                type: "syncKingdeeTimeDialogBn",
                tourist: true
            }
        },
        {
            path: '/syncTime',
            name: 'syncTime',
            component: () => import('../../src/views/jikeyun/syncTime.vue'),
            meta: {
                type: "syncTime",
                tourist: true
            }
        }
    ]
    history = createWebHashHistory()

    constructor(options = {}) {
        this.routes = this.routes.concat(options.routes || [])
        this.router = createRouter({
            history: this.history,
            routes: this.routes
        })
        /**
         * 动态加载路由
         * @param list
         */
        let routerData = sessionStorage.getItem("_routerData")
        if (routerData) {
            routeMixin.methods.initRouterUrl(JSON.parse(routerData), this.router, options.store)
        }
        this.router.beforeEach((to, from, next) => {
            if (to.fullPath === "/" && options.store && options.store.getters.getDefaultRoutePath) {
                next({
                    path: options.store.getters.getDefaultRoutePath
                })
            } else {
                if (!to.meta.tourist && options.store) {
                    if (sessionStorage.token) {
                        options.store.commit('recoveryPage')
                        if (to.meta.newWindow) {
                            options.store.commit('addPageItem', to)
                        }
                        next();
                    } else {
                        next({path: '/login'});
                    }
                } else {
                    next();
                }
            }
            return true
        })
    }

    init() {
        return this.router
    }
}
